<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(S) {
      const result = [];
      function dfs(i, str) {
        if (i === S.length) result.push(str.join(''));
        for (let j = i; j < S.length; j++) {
          [str[i], str[j]] = [str[j], str[i]];
          dfs(i + 1, [...str]);
          [str[i], str[j]] = [str[j], str[i]];
        }
      }
      dfs(0, S.split(''));
      return result;
    };
    console.log(arr("qwe"));
  </script>
</body>

</html>